<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
        2 3 1 5 4 6
        11 5 4 6
        定义两个指针，计算出两个链表长度的差值，因为相差的地方一定不是相交点
        2 3 1 5 4 6
            11 5 4 6
        */
      var getIntersectionNode = function (headA, headB) {
        let curA = headA;
        let curB = headB;
        let lengthA = 0;
        let lengthB = 0;
        let pA = headA;
        let pB = headB;
        while (curA) {
          lengthA++;
          curA = curA.next;
        }
        while (curB) {
          lengthB++;
          curB = curB.next;
        }
        //交换链表，一定让长的在上面
        if (lengthA < lengthB) {
          [lengthA, lengthB] = [lengthB, lengthA];
          [pA, pB] = [pB, pA];
        }
        let len = lengthA - lengthB;
        while (len--) {
          pA = pA.next;
        }
        while (pA && pB) {
          if (pA == pB) {
            return pA;
          }
          pA = pA.next;
          pB = pB.next;
        }
        return pA;
      };
    </script>
  </body>
</html>
